package com.molamil.outboard.shortcut {
	import flash.ui.Keyboard;
	import com.molamil.outboard.Outboard;
	import com.molamil.outboard.IShortcut;

	/**
	 * @author martindyrby
	 */
	public class KeyboardPositionShortcut implements IShortcut {
		
		public var target:*;
		public var name:String;
		public var precision : uint;
				
		private var upIndex:int;
		private var downIndex:int;
		private var leftIndex:int;
		private var rightIndex:int;
		private var outboard:Outboard;


		
		public function init(outb:Outboard) : void {
			outboard = outb;
			
			upIndex = outboard.onKeyUp(Keyboard.UP, onUp);
			downIndex = outboard.onKeyUp(Keyboard.DOWN, onDown);
			leftIndex = outboard.onKeyUp(Keyboard.LEFT, onLeft);
			rightIndex = outboard.onKeyUp(Keyboard.RIGHT, onRight);
		}
		
		public function reactivate() : void {
			outboard.reactivateListener(upIndex);
			outboard.reactivateListener(downIndex);
			outboard.reactivateListener(leftIndex);
			outboard.reactivateListener(rightIndex);	
		}

		public function deactivate() : void {
			outboard.deactivateListener(upIndex);
			outboard.deactivateListener(downIndex);
			outboard.deactivateListener(leftIndex);
			outboard.deactivateListener(rightIndex);			
		}

		private function onRight() : void {
			target.x += (precision > 0) ? (1/(precision*10)) : 1;
			trace(name, "x", target.x);
		}

		private function onLeft() : void {
			target.x -= (precision > 0) ? (1/(precision*10)) : 1;
			trace(name, "x", target.x);
		}

		private function onDown() : void {
			target.y += (precision > 0) ? (1/(precision*10)) : 1;
			trace(name, "y", target.y);
		}

		private function onUp() : void {
			target.y -= (precision > 0) ? (1/(precision*10)) : 1;
			trace(name, "y", target.y);
		}
	}
}
